#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
填充测试数据脚本
检查数据库中是否已经存在测试数据，如果没有则填充
"""

import os
import django
from datetime import date

# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'vul_project.settings')
django.setup()

from vul_app.models import Author, Book

def check_if_data_exists():
    """检查是否已经存在测试数据"""
    author_count = Author.objects.count()
    book_count = Book.objects.count()
    return author_count > 0 and book_count > 0

def populate_test_data():
    """填充测试数据"""
    # 创建测试作者
    author1 = Author.objects.create(
        name="张三",
        age=35,
        email="zhangsan@example.com",
        phone="13800138001",
        ssn="11010112345",
        credit_card="6222021234567890",
        api_key="sk_test_123456789"
    )
    
    author2 = Author.objects.create(
        name="李四",
        age=40,
        email="lisi@example.com",
        phone="13800138002",
        ssn="11010112346",
        credit_card="6222021234567891",
        api_key="sk_test_123456790"
    )
    
    author3 = Author.objects.create(
        name="王五",
        age=45,
        email="wangwu@example.com",
        phone="13800138003",
        ssn="11010112347",
        credit_card="6222021234567892",
        api_key="sk_test_123456791"
    )
    
    # 创建测试书籍
    books_data = [
        {
            "isbn": "9787111234567",
            "name": "Django实战指南",
            "pages": 350,
            "rating": 4.5,
            "price": 59.99,
            "contact": author2,
            "pubdate": date(2023, 1, 15),
            "secret_code": "DJ2023",
            "drm_key": "drm_123456"
        },
        {
            "isbn": "9787111345678",
            "name": "Python编程思想",
            "pages": 420,
            "rating": 4.8,
            "price": 79.99,
            "contact": author2,
            "pubdate": date(2022, 6, 20),
            "secret_code": "PY2022",
            "drm_key": "drm_123457"
        },
        {
            "isbn": "9787111456789",
            "name": "未来世界",
            "pages": 280,
            "rating": 4.2,
            "price": 39.99,
            "contact": author1,
            "pubdate": date(2021, 11, 5),
            "secret_code": "FW2021",
            "drm_key": "drm_123458"
        },
        {
            "isbn": "9787111567890",
            "name": "数据库设计与优化",
            "pages": 380,
            "rating": 4.6,
            "price": 69.99,
            "contact": author3,
            "pubdate": date(2024, 3, 10),
            "secret_code": "DB2024",
            "drm_key": "drm_123459"
        },
        {
            "isbn": "9787111678901",
            "name": "Web安全实践",
            "pages": 450,
            "rating": 4.7,
            "price": 89.99,
            "contact": author1,
            "pubdate": date(2023, 8, 30),
            "secret_code": "WS2023",
            "drm_key": "drm_123460"
        }
    ]
    
    for book_data in books_data:
        Book.objects.create(**book_data)
    
    print(f"成功创建 {Author.objects.count()} 个作者和 {Book.objects.count()} 本书籍")

if __name__ == "__main__":
    if check_if_data_exists():
        print("数据库中已存在测试数据，无需重复填充")
    else:
        print("数据库中无测试数据，开始填充...")
        populate_test_data()

